package org.huangrui.spark.scala.core.rdd.operate.transform

import org.apache.spark.{SparkConf, SparkContext}

/**
 * @Author hr
 * @Create 2024-10-17 22:58 
 */
object Spark13_Operate_Transform_KV_sortByKey_2 {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setMaster("local[*]").setAppName("spark")
    val sc = new SparkContext(conf)
    val tuples = List(("a", 1), ("a", 3), ("a", 4), ("a", 2))
    // (a, 1) => (1, (a, 1))
    // (a, 3) => (3, (a, 3))
    // (a, 4) => (4, (a, 4))
    // (a, 2) => (2, (a, 2))
    sc.parallelize(tuples,2).map(t => (t._2, t)).sortByKey(ascending = true,1).map(t => t._2).foreach(println)

    sc.parallelize(tuples,2).map(t => (t._2, t._1)).sortByKey(ascending = false,1).map(t => (t._2,t._1)).foreach(println)
    sc.stop()
  }
}
